package yz;

import java.util.LinkedList;

/**
 * [剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)
 *
 * @Author yangz
 * @Date 2022/11/11 20:17
 */
public class LeetCode19_YongLiangGeZhanShiXianDuiLieLcof {
    LinkedList<Integer> in, out;

    public LeetCode19_YongLiangGeZhanShiXianDuiLieLcof() {
        in = new LinkedList<Integer>();
        out = new LinkedList<Integer>();
    }

    // in 栈元素全部移到 out 栈 的方法及逻辑判断抽取出来
    private void transferIfEmpty() {
        // in空 非法移动，直接结束
        if (in.isEmpty()) return;
        if (out.isEmpty()) {
            while (!in.isEmpty()) {
                out.push(in.pop());
            }
        }
    }

    // in 栈负责进
    public void appendTail(int value) {
        in.addLast(value);
    }

    // out 栈负责出
    public int deleteHead() {
        transferIfEmpty();
        if (!out.isEmpty()) {
            return out.removeLast();
        }
        return -1;
    }
}
